package com.weixin.servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.weixin.service.IUser_tableService;
import com.weixin.service.impl.User_tableServiceImpl;
import com.weixin.util.PageUtil;
import com.weixin.vo.Member;
import com.weixin.vo.MemberList;

/**
 * Servlet implementation class User_tableServlet
 */
@WebServlet("/User_tableServlet/*")
public class User_tableServlet extends HttpServlet {
	
	IUser_tableService User_tableService = new User_tableServiceImpl();
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String PathInfo = request.getPathInfo().substring(1);
		 if("edit".equals(PathInfo)) {
			edit(request,response);
		}else if("preChongZhi".equals(PathInfo)) {
			preChongZhi(request,response);
		}else if("jihuo".equals(PathInfo)) {
			jihuo(request,response);
		}else if("selectAllInfoAndSearch".equals(PathInfo)) {
			selectAllInfoAndSearch(request,response);
		}
		
		
		
		
	}
	private void selectAllInfoAndSearch(HttpServletRequest request, HttpServletResponse response) {
		try {
			request.setCharacterEncoding("utf-8");
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/htmlc;charset=utf-8");
		
		int user_id = 0;
		if(request.getParameter("user_id")!=null&&!(request.getParameter("user_id").equals(""))) {
			user_id = Integer.valueOf(request.getParameter("user_id"));
		}
		//int user_id =1001;
		int pageNum = 1;
		if(request.getParameter("pageNum")!=null) {
			pageNum = Integer.valueOf(request.getParameter("pageNum"));
		}
		//设置每页条数
		int pageSize = 6;
		try {
			//调用业务层方法进行分页搜索操作
			List<Member> selectInfoAndSearch = User_tableService.selectInfoAndSearch(user_id,pageNum,pageSize);
			//获得总数据数
			int total = User_tableService.findTotal();
			//获得总页数
			int pageCount = (int)Math.ceil(total*1.0/pageSize);
			//绑定user_id属性进行表单回填
			request.setAttribute("selectInfoAndSearch", selectInfoAndSearch);
			request.setAttribute("user_id", user_id);
			request.setAttribute("pageNum",pageNum);
			request.setAttribute("pageCount",pageCount);
			//跳转到jsp页面
			request.getRequestDispatcher("/pages/member/huiyuan_list.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	private void jihuo(HttpServletRequest request, HttpServletResponse response) {
		//获取会员编号
		int user_id = 0;
		if(request.getParameter("user_id")!=null&&!("".equals(request.getParameter("user_id")))) {
			user_id = Integer.valueOf(request.getParameter("user_id"));
		}
		//获取状态id
		int acti_id = Integer.valueOf(request.getParameter("acti_id"));
		try {
			if(acti_id==0) {
				
				boolean b = User_tableService.editActi_id(user_id, 1);
				if(b) {
					response.getWriter().println(1);
				}else {
					response.getWriter().println(0);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	private void preChongZhi(HttpServletRequest request, HttpServletResponse response) {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/htmlc;charset=utf-8");
		//获取会员编号
		int user_id = 0;
		if(request.getParameter("user_id")!=null&&!("".equals(request.getParameter("user_id")))) {
			user_id = Integer.valueOf(request.getParameter("user_id"));
		}
		try {
			//调用业务层方法获取需要充值的会员信息
			Member member = User_tableService.findById(user_id);
			response.getWriter().println(new Gson().toJson(member));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	private void edit(HttpServletRequest request, HttpServletResponse response) {
		//获取充值金额
		double money = 0;
		if(request.getParameter("money")!=null&&!("".equals(request.getParameter("money")))) {
			money = Integer.valueOf(request.getParameter("money"));
		}
		//获取会员余额
		double user_money = 0;
		if(request.getParameter("user_money")!=null&&!("".equals(request.getParameter("user_money")))) {
			user_money = Integer.valueOf(request.getParameter("user_money"));
		}
		//获取会员编号
		int user_id = 0;
		if(request.getParameter("user_id")!=null&&!("".equals(request.getParameter("user_id")))) {
			user_id = Integer.valueOf(request.getParameter("user_id"));
		}
			
		try {
			//调用业务层方法查看充值是否成功
			boolean b = User_tableService.addMoney(money, user_money, user_id);
			if(b) {
				response.getWriter().println(1);
			}else {
				response.getWriter().println(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}


}
